package cn.jy.map.utils;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author XiongJing
 * @version 1.0
 * @date 2018年7月8日 上午12:06:26 类说明 ：时间工具类
 */
public class DateUtils {
	/**
	 * 传入一个时间,得到当年的第一个月的第一天 如2018-06-15==>> 2018-01-01
	 * 
	 * @param date
	 * @return
	 */
	public static Date getYearFirstDayOfTheFrstMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取上一年的最后一个月的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastYearLastDayOfTheLastMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.MONTH, 11);
		calendar.set(Calendar.DAY_OF_MONTH, 31);
		return calendar.getTime();

	}

	public static Date getNextRound(Date date, Integer num) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, +num);
		return calendar.getTime();
	}

	/**
	 * 得到轮次的时间,14天为一个轮次那么加14天
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date getRoundTime(Date date, Integer day) {
		long time = date.getTime();
		day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
		time += day; // 相加得到新的毫秒数
		return new Date(time); // 将毫秒数转换成日期

	}

	/**
	 * 得到第一轮次的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getfirstRoundTime(Date date) {
		Date newDate = getYearFirstDayOfTheFrstMonth(date);
		Date oldDate = getLastYearLastDayOfTheLastMonth(newDate);
		Date firstRoundTime = getRoundTime(oldDate, 14);
		return firstRoundTime;

	}

	/**
	 * 格式化时间 如'2018-06-13'
	 * 
	 * @param date
	 * @return
	 */

	public static String getSimpleDateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	/**
	 * 传入轮次的初始时间，得到轮次的截止时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getRoundEndTime(Date date) {
		long time = date.getTime();
		Integer day = 13 * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
		time += day; // 相加得到新的毫秒数
		return new Date(time); // 将毫秒数转换成日期;

	}

	/**
	 * 传入轮次截止时间，来得到下一轮次的初始时间
	 * 
	 * @param endTime
	 * @return
	 */
	public static Date getNextDay(Date date) {
		long time = date.getTime();
		Integer day = 1 * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
		time += day; // 相加得到新的毫秒数
		return new Date(time); // 将毫秒数转换成日期;

	}

	/**
	 * 传入一个时间，得到上一年的01-01
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, -1);
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	/**
	 * 传入一个时间，得到当前月的第一天 比如2018-06-01
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfTheMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	/**
	 * 传入一个时间，得到当前月的最后一天 比如2018-06-30
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfTheMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// calendar.add(Calendar.MONTH, -1);
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	/**
	 * 得到任意年
	 * 
	 * @param date
	 * @return
	 */
	public static Date getAnyYears(Date date, Integer year) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	/**
	 * @param date
	 *            传入一个当前时间的任意年任意月任意一天
	 * @param year
	 *            0表示当前年，负数表示以前，正数未来年
	 * @param month
	 *            值范围 0-11
	 * @param day
	 *            值范围 1-31
	 * @return
	 */
	public static Date getAnyDayAnyYearAnyMonth(Date date, Integer year,
			Integer month, Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}

	public static String getAnyDayAnyYearAnyMonthString(Date date,
			Integer year, Integer month, Integer day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		return getSimpleDateFormat(calendar.getTime());
	}

	/**
	 * 传入一个年，判断是否是闰年 如果是闰年则有366天，否者只有365天
	 * 
	 * @param date
	 * @return
	 */
	public static Boolean getLeapYear(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		if (Integer.parseInt(sdf.format(date)) % 4 == 0
				&& Integer.parseInt(sdf.format(date)) % 100 != 0
				|| Integer.parseInt(sdf.format(date)) % 400 == 0) {
			// 是闰年
			return true;
		} // 不是闰年
		return false;
	}

	/**
	 * 传入2个数计算结果得到百分比！ 输入的参数：num1 < num2 例如 249/962 = 26%
	 * 
	 * @param d
	 * @return getPercentage
	 */
	public static String getPercentage(double num1, double num2) {
		NumberFormat nf = NumberFormat.getPercentInstance();
		if (num1 < num2) {
			double num3 = num1 / num2;
			return nf.format(num3);
		}
		String error = "输入的参数:" + "num1" + "<" + "num2" + ",请重新输入!";
		return error;
	}

	/**
	 * 传入一个日期字符串把他转为时间
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static Date getDate(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		return sdf.parse(date);
	}

	/**
	 * 传入一个时间获取当前月的最后一天
	 * 
	 * @param date
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getMonthLastDay(Date date, Integer year, Integer month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return getSimpleDateFormat(calendar.getTime());
	}

	/**
	 * 传入一个日期时间，得到昨天
	 * 
	 * @param date
	 * @return
	 */
	public static String getYesterdayTime(Date date, Integer num) {
		long time = date.getTime();
		Integer day = num * 1000 * 60 * 60 * 24;
		time -= day;
		Date yesterday = new Date(time);
		return getSimpleDateFormat(yesterday);

	}

	/**
	 * 将时间转换为字符串
	 * 
	 * @param date
	 * @return 2018-01
	 */
	public static String getString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		return sdf.format(date);
	}

	/**
	 * 计算结果保留2位小数
	 * 
	 * @param num1
	 * @param num2
	 * @return
	 */
	public static String getDigital(double num1, double num2) {
		Double num = num1 / num2;
		NumberFormat nf = NumberFormat.getNumberInstance();
		// 保留两位小数
		nf.setMaximumFractionDigits(2);
		// 如果不需要四舍五入，可以使用RoundingMode.DOWN
		nf.setRoundingMode(RoundingMode.UP);
		return nf.format(num);
	}

	/**
	 * 时间格式化为2018
	 */
	public static String getYear(Date date, Integer num) {
		SimpleDateFormat format = new SimpleDateFormat("YYYY");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, num);
		return format.format(calendar.getTime());
	}

	/**
	 * @param str
	 * 获取月份
	 * @return
	 */
	public static Integer getNumber(String str) {
		String[] str2 = { "01", "02", "03", "04", "05", "06", "07", "08", "09" };
		String[] str3 = { "10", "11", "12", "00", "00", "00", "00", "00", "00" };
		int number = 0;
		for (int i = 0; i <= str2.length - 1; i++) {
			if (str.substring(5, 7).equals(str2[i])) {
				number = Integer.parseInt(str.substring(5, 7).substring(1));
			}
			if (str.substring(5, 7).equals(str3[i])) {
				number = Integer.parseInt(str.substring(5, 7));
			}
		}
		return number;
	}

	/**
	 * 传入一个时间判断是否是星期天
	 */
	public static Boolean getDayOfWeek(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date tmpDate;
		try {
			tmpDate = format.parse(date);
			Calendar cal = Calendar.getInstance();
			cal.setTime(tmpDate);
			int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
			if (1 == dayWeek) {
				return true;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static String getTime(String startTime, String endTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d1 = null;
		Date d2 = null;
		String massage = null;
		try {
			d1 = format.parse(startTime);
			d2 = format.parse(endTime);
			// 毫秒ms
			long diff = d2.getTime() - d1.getTime();
			long diffSeconds = diff / 1000 % 60;
			long diffMinutes = diff / (60 * 1000) % 60;
			long diffHours = diff / (60 * 60 * 1000) % 24;
			long diffDays = diff / (24 * 60 * 60 * 1000);
			massage = diffMinutes + "分" + diffSeconds + "秒";
			// System.out.print("两个时间相差：");
			// System.out.print(diffDays + " 天, ");
			// System.out.print(diffHours + " 小时, ");
			// System.out.print(diffMinutes + " 分钟, ");
			// System.out.print(diffSeconds + " 秒.");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return massage;
	}

	public static void main(String[] args) throws Throwable {
	
		Integer number = DateUtils.getNumber(DateUtils.getSimpleDateFormat(new Date()));
		System.out.println(number);
	}

}
